/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package genetic;

import hash.CellSpace;
import image.ImageSet;

import java.util.ArrayList;
import java.util.Iterator;

import configuration.GenePoolConfiguration;

/**
 * @author Ashish
 *
 */
public class Population {
	private ArrayList<Chromosome> chromosomes = new ArrayList<Chromosome>();
	
	/**
	 * @return Chromosome, the fittest chromosome in the population
	 */
	public Chromosome selectFitesttestChromosome(){
		
		int fittestChromosomeIdx = 0;
		double maxfitness = this.chromosomes.get(fittestChromosomeIdx).getFitness();
		for(int i = 0; i < this.chromosomes.size(); i++){
			if(this.chromosomes.get(i).getFitness() > maxfitness){
				fittestChromosomeIdx = i;
				maxfitness = this.chromosomes.get(fittestChromosomeIdx).getFitness();
			}
		}
		return this.chromosomes.get(fittestChromosomeIdx);
	}
	/**
	 * @param chromosomes the chromosomes to set
	 */
	public void setChromosomes(ArrayList<Chromosome> chromosomes) {
		this.chromosomes = chromosomes;
	}

	/**
	 * @return the chromosomes
	 */
	public ArrayList<Chromosome> getChromosomes() {
		return chromosomes;
	}
	
	public Population evolve(Population oldPopulation, GenePoolConfiguration geneConfig, CellSpace cellSpace, ImageSet imageSet){
		Population newPopulation = new Population(geneConfig);
		Iterator<Chromosome> itr = oldPopulation.getChromosomes().iterator();
		while(itr.hasNext()){
			Chromosome newChromosome = new Chromosome(itr.next(),geneConfig, cellSpace, imageSet); // constructor of chromosome during evolution
			newPopulation.getChromosomes().add(newChromosome);
		}
		return newPopulation;
	}
	
	public Population(GenePoolConfiguration geneConfig, CellSpace cellSpace, ImageSet imageSet){
		chromosomes = new ArrayList<Chromosome>(geneConfig.getNumChromosomes());
		for(int i=0; i < geneConfig.getNumChromosomes();i++){
			Chromosome chromosome = new Chromosome(geneConfig, cellSpace, imageSet);
			chromosomes.add(chromosome);
		}
	}
	/**
	 * @param geneConfig 
	 * 
	 */
	public Population(GenePoolConfiguration geneConfig) {
		this.chromosomes = new ArrayList<Chromosome>(geneConfig.getNumChromosomes());
	}
	
}
